<template>
	<view class="resume-detail">
		<uv-form labelPosition="left" :labelStyle="{ color: '#767a82' }" labelWidth="80" :model="form">
			<uv-form-item label="面试岗位" borderBottom>
				<uv-input disabled v-model="form.jobName"></uv-input>
			</uv-form-item>
			<uv-form-item label="姓名" borderBottom><uv-input disabled v-model="form.name"></uv-input></uv-form-item>
			<uv-form-item label="性别" borderBottom><uv-input disabled v-model="form.sex"></uv-input></uv-form-item>
			<uv-form-item label="出生年月" borderBottom>
				<uv-input disabled v-model="form.birth"></uv-input>
			</uv-form-item>
			<uv-form-item label="学院" borderBottom><uv-input disabled v-model="form.college"></uv-input></uv-form-item>
			<uv-form-item label="专业" borderBottom><uv-input disabled v-model="form.major"></uv-input></uv-form-item>
			<uv-form-item label="在校经历" borderBottom>
				<uv-textarea disabled v-model="form.experience" autoHeight border="none"></uv-textarea>
			</uv-form-item>
			<uv-form-item label="专业技能" borderBottom>
				<uv-textarea disabled v-model="form.skill" autoHeight border="none"></uv-textarea>
			</uv-form-item>
			<uv-form-item label="简历信息" borderBottom>
				<uv-button type="primary" text="查看简历" @click="handleShowResume"></uv-button>
			</uv-form-item>
		</uv-form>
		<view class="action">
			<view class="send collection" @click="handleChat">沟通</view>
			<view class="inquiry" :class="{ waring: apply }" @click="toPage">{{ apply ? '已预约' : '面试预约' }}</view>
		</view>
	</view>
</template>

<script>
import socketManager from '@/utils/websocket.js';
import { updateDeliveryStatus } from '@/apis/delivery/index.js';
import { getApplyList } from '@/apis/apply/apply.js';

export default {
	data() {
		return {
			form: {},
			applyList: []
		};
	},
	computed: {
		// 面试预约
		apply() {
			const { id, jobName } = this.form;
			return this.applyList.find(item => {
				if (item.friendId === id && item.jobName === jobName) {
					return item;
				}
				return false;
			});
		}
	},
	onLoad() {
		const resume = uni.getStorageSync('resume');
		this.form = JSON.parse(resume);
		this.form.birth = this.timeFormat(this.form.birth);
		// 更新简历状态
		updateDeliveryStatus({ type: 'one', id: this.form.deliveryId });
	},
	onShow() {
		this.initData();
	},
	onUnload() {
		uni.removeStorageSync('resume');
	},
	methods: {
		async initData() {
			const userId = this.$store.state.userInfo.id;
			const res = await getApplyList(userId);
			this.applyList = res.data;
		},
		// 格式化时间
		timeFormat(value) {
			return uni.$uv.timeFormat(value);
		},
		// 跳转到聊天界面
		handleChat() {
			const socket = socketManager.getSocket();
			const userId = this.$store.state.userInfo.id;
			// 添加好友
			socket.emit('addFriend', { userId, friendId: this.form.id });
			this.$store.commit('SET_ACTIVE_ROOM', this.form.id);
			uni.navigateTo({ url: `/pages/chat/chat` });
		},
		// 查看简历信息
		handleShowResume() {
			uni.downloadFile({
				url: this.form.url,
				success: function(res) {
					var filePath = res.tempFilePath;
					uni.openDocument({
						filePath: filePath,
						showMenu: true,
						success: function(res) {
							console.log('打开文档成功');
						}
					});
				}
			});
		},
		toPage() {
			const { id, jobName } = this.form;
			if (this.apply) {
				uni.navigateTo({ url: `/pages/company/interview?friendId=${id}&name=${jobName}&id=${this.apply.id}` });
			} else {
				uni.navigateTo({ url: `/pages/company/interview?friendId=${id}&name=${jobName}` });
			}
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #fff;
}
.uv-textarea {
	background-color: #fff !important;
}
</style>

<style lang="scss" scoped>
.resume-detail {
	padding: 0 30rpx;
	.action {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 130rpx;
		padding: 0 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #fff;
		box-shadow: 0 -16rpx 20rpx -10rpx rgba(0, 0, 0, 0.1);
		view {
			height: 90rpx;
			line-height: 90rpx;
			border-radius: 10rpx;
			text-align: center;
			display: inline-block;
		}
		.collection {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-right: 20rpx;
			font-size: 32rpx;
			background-color: #f4f4f4;
			.label-icon {
				font-size: 40rpx;
				margin-right: 10rpx;
			}
		}
		.inquiry {
			flex: 2;
			font-size: 36rpx;
			color: #fff;
			background-color: $main-color;
		}
		.waring {
			background-color: #f9ae3d;
		}
	}
}
</style>
